package com.dailywork.basepro.thread.provider;

import java.util.Random;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public class Provider implements Runnable {

    // ��������
    private BlockingQueue<Data> queue;
    // ���̼߳��Ƿ���������,��ǿ�ƴ����ڴ���ˢ�µĹ���,��ʱ�����̵߳�״̬
    private volatile boolean isRunning = true;
    // id������
    private static AtomicInteger count = new AtomicInteger();
    // �������
    private static Random r = new Random();

    public Provider(BlockingQueue<Data> queue) {
        this.queue = queue;
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        while (isRunning) {
            try {
                // �������0-1000����,��ʾ��ȡ����(�������ݵĺ�ʱ)
                Thread.sleep(r.nextInt(1000));
                // ��ȡ�����ݽ����ۼ�
                int id = count.getAndIncrement();
                // ����ͨ��һ��getData������ȡ��
                Data data = new Data(Integer.toString(id), "����" + id);
                System.out.println(
                        "��ǰ�߳�:"
                                + Thread.currentThread().getName()
                                + ",��ȡ������,idΪ:"
                                + id
                                + ",����װ�ص�������...");
                if (!this.queue.offer(data, 2, TimeUnit.SECONDS)) {
                    System.out.println("�ύ����������ʧ��...");
                }

            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    public void stop() {
        this.isRunning = false;
    }
}
